namespace Pay.Core {
	using com.google.zxing;
	using com.google.zxing.qrcode.decoder;
	using System;
	using System.Collections;
	using System.Drawing;
	using System.Drawing.Drawing2D;
	using System.Drawing.Imaging;
	using ThoughtWorks.QRCode.Codec;

	public class QRCodeHelper {
        public static Bitmap Create(string content) {
            MultiFormatWriter writer = new MultiFormatWriter();
            return writer.encode(content, BarcodeFormat.QR_CODE, 300, 300).ToBitmap();
        }

        public static Bitmap Create(string content, Image centralImage) {
            MultiFormatWriter writer = new MultiFormatWriter();
            Hashtable hints = new Hashtable();
            hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
            hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            Bitmap bitmap = writer.encode(content, BarcodeFormat.QR_CODE, 300, 300, hints).ToBitmap();
            Image image = centralImage;
            Size size = writer.encode(content, BarcodeFormat.QR_CODE, 300, 300).ToBitmap().Size;
            int width = Math.Min((int)(((double)size.Width) / 3.5), image.Width);
            int height = Math.Min((int)(((double)size.Height) / 3.5), image.Height);
            int x = (bitmap.Width - width) / 2;
            int y = (bitmap.Height - height) / 2;
            Bitmap bitmap2 = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format32bppArgb);
            using (Graphics graphics = Graphics.FromImage(bitmap2)) {
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.DrawImage(bitmap, 0, 0);
            }
            Graphics graphics2 = Graphics.FromImage(bitmap2);
            graphics2.FillRectangle(Brushes.White, x, y, width, height);
            graphics2.DrawImage(image, x, y, width, height);
            return bitmap2;
        }

        public static Bitmap Create(string content, string imagePath) {
            Image centralImage = Image.FromFile(imagePath);
            Bitmap bitmap = Create(content, centralImage);
            centralImage.Dispose();
            return bitmap;
        }

		/// <summary>  
		/// 生成二维码图片  
		/// </summary>  
		/// <param name="codeNumber">要生成二维码的字符串</param>       
		/// <param name="size">二维码每个颗粒大小尺寸</param>  
		/// <returns>二维码图片</returns>  
		public static Bitmap CreateImgCode(string codeNumber, int size) {
			//创建二维码生成类  
			QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
			//设置编码模式  
			qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
			//设置编码测量度  
			qrCodeEncoder.QRCodeScale = size;
			//设置编码版本  
			qrCodeEncoder.QRCodeVersion = 0;
			//设置编码错误纠正  
			qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
			//生成二维码图片  
			System.Drawing.Bitmap image = qrCodeEncoder.Encode(codeNumber);

			return image;
		}
    }
}

